# -*- coding: utf-8 -*-
# @Time    : 2019/3/13 14:38
# @Author  : Macher
# @File    : emailsimiliar.py
# @Software: PyCharm Community Edition
#计算邮箱相似度问题
import mysql.connector
import re
import Levenshtein

config = {'host': '219.216.65.41',
          'user': 'root',
          'password': '123456',
          'port': 3306,
          'database': 'mydata',
          'charset': 'utf8'}



def getEmail():
    conn = mysql.connector.connect(**config)
    cursor = conn.cursor()
    cursor.execute("SELECT id,email FROM malware_email")
    results = cursor.fetchall()
    cursor.close()
    conn.commit()
    conn.close()
    return results

def dealDict(results):
    dict1 = {}
    for row in results:
        # print row[0]
        # print row[1].split('@')[0]
        if len(re.compile(r'@').findall(row[1])) !=0:
            dict1[row[0]] = row[1].split('@')[0]
        else:
            continue
        # new.append(row)
    # new = list(set(dealString(row[1].split('@')[0])))

    print 'get email done'
    return dict1


def compare(d1,num):
    l1 = d1.keys()
    d2 = {}
    while l1:
        l2 = []
        item = l1.pop()
        for i in l1:
            # print d1[item], d1[i]
            # print i
            if float(Levenshtein.jaro_winkler(d1[item], d1[i])) > num:
                l2.append(i)
        l2.append(item)
        d2[item] = l2
            # print countSame(d1[i],d1[item])
    # print d2
    l3 = []
    for key in d2:
        l3.append(d2[key])
    print 'compare done'
    return l3

def Merge_collection(l3):
# l3 = [[1], [1, 2], [3], [3, 4], [5], [5, 6], [5, 6, 7], [5, 6, 7, 8], [5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10]]
    l4 = []
    for i in l3:
        maxl = i
        for j in l3:
            if set(i) < set(j):
                maxl = j
        if maxl not in l4:
            l4.append(maxl)
    print 'Merge_collection done'
    return l4

def Email(results,l2):
    l4 = []
    l1 = []
    total = {}
    dict1 = {}
    for row in results:
        dict1[row[0]] = row[1]
    for item in l2:
        l3 = []
        for num in item:
            if dict1[num] not in l3:
                l3.append(dict1[num])
        l1.append((l3))
    # print l1
    for em in l1:
        if len(em)>1:
            l4.append(em)
    # print 'total:',len(l4)
    for emails in l4:
        # print emails
        # print len(emails)
        if total.has_key(len(emails)):
            temp = total[len(emails)]
            temp += 1
            total[len(emails)] = temp
        else:
            total[len(emails)] = 1
    print 'total:', len(l4)
    print total



i = 0.70
results = getEmail()
d1 = dealDict(results)
while i >0.6 :
    print i
    l2 = Merge_collection(compare(d1,i))
    Email(results,l2)
    i -=0.01
